package leader.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import leader.service.DirectAwardService;
import leader.service.DirectPatentService;

import com.opensymphony.xwork2.ActionContext;

import teacher.bean.Patent;
import teacher.bean.Teacher;

public class DirectPatentLikeNameAction {
	private List<Patent> patent;
	private String patName;
	private int currentpage;
	private int endpage;
	private int count;
	private int pagesize;
	private int flag;
	private int code;
	public List<Patent> getPatent() {
		return patent;
	}
	public void setPatent(List<Patent> patent) {
		this.patent = patent;
	}
	public String getPatName() {
		return patName;
	}
	public void setPatName(String patName) {
		this.patName = patName;
	}
	public int getCurrentpage() {
		return currentpage;
	}
	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public int getEndpage() {
		return endpage;
	}
	public void setEndpage(int endpage) {
		this.endpage = endpage;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public int getPagesize() {
		return pagesize;
	}
	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}
	public int getFlag() {
		return flag;
	}
	public void setFlag(int flag) {
		this.flag = flag;
	}
	public String execute() {
		
		ActionContext.getContext().put("flag", 2);
	    ActionContext.getContext().put("name", this.getPatName());
		
		Teacher lea=(Teacher) ActionContext.getContext().getSession().get("leader");
		DirectPatentService pat = new DirectPatentService();
		if(this.getCode()==1) {
			try {
				patName = new String(patName.getBytes("iso-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(currentpage);
		pagesize = 10;
		count = pat.getCountOfDirectPatentByName(lea, patName);
    	if(currentpage == 0) {
    		currentpage = 1;
    	}
    	int add = 0;
    	if(count%pagesize != 0) {
    		add = 1;
    	}
    	endpage = count/pagesize + add;
    	if(currentpage > endpage && endpage > 0) {
    		currentpage = endpage;
    	}
    	ActionContext.getContext().put("flag",2);
    	flag = 2;
		patent = pat.selectSegregatePatentLikeName(patName, lea,(currentpage-1)*pagesize+1, pagesize);
		return "success";
	}
}
